define(function (require) {

    var $ = require("jquery");
    require("jquery.cookie");

    function initHTML() {
        var userType = $.cookie("type");
        var name = $.cookie("name");

        // header
        var selfInfoDiv = document.querySelector(".self-info");
        selfInfoDiv.querySelector(".type").innerText = userType === "teacher" ? "Tch" : "Stu";
        selfInfoDiv.querySelector(".name").innerText = name;


        // nav
        var navUl = document.querySelector("nav>ul");
        if (userType === "student") {
            navUl.querySelectorAll(".switcher [class~=switcher__]").forEach(function (item) {
                item.innerHTML = "已" + item.innerHTML;
                item.classList.add("disabled");
            });

            var tmp = navUl.querySelector(".drag-box");
            tmp.removeChild(tmp.querySelector(".drag-box__container"));
            tmp.classList.add("disabled")
        }

        //bottom-bar


    }

    function initComponents() {
        var components = require("components");

        var navUl = document.querySelector("nav>ul");
        var userHandler = new require("userHandler").UserHandler(),
            teacherHandler = new require("userHandler").TeacherHandler(),
            studentHandler = new require("userHandler").StudentHandler();

        var userType = $.cookie("type");
        var courseId = $.cookie("courseId");

        // 初始化当前课程
        var dragBoxLi = navUl.querySelector(".drag-box");
        if (userType === "teacher") {

            // 初始化当前课程
            var courseContainer = dragBoxLi.querySelector(".drag-box__container");
            courseContainer.innerHTML = '<a href="javascript:void(0)" class="drag-box__bottom-bar">+添加课程</a>';
            var courseAddButton = courseContainer.querySelector(".drag-box__bottom-bar");
            // 初始化课程列表
            userHandler.getAllCourse(function (data) {
                for (var i in data) {
                    var tmp = document.createElement("LI");
                    tmp.classList.add("drag-box__item");
                    var tmp2 = document.createElement("A");
                    tmp2.href = "javascript:void(0)";
                    tmp2.innerText = data[i].name;
                    tmp.appendChild(tmp2);
                    tmp.addEventListener("click",(function (id) {
                        return function () {
                            $.cookie('courseId',id);
                            location.reload();
                        }
                    }(data[i].id)));
                    courseContainer.insertBefore(tmp, courseAddButton);
                }
            });
            // 绑定AddCourse事件
            courseAddButton.addEventListener("click", function () {
                // 弹窗
                require("popups").createAddCoursePopup();
            })

        } else {

            // 只需要根据courseId找出courseName
        }


        // 初始化3个状态
        var courseStateDiv = document.getElementById("course-state"),
            liveStateDiv = document.getElementById("live-state"),
            chatStateDiv = document.getElementById("chat-state");

        var courseSwitcher = new components.Switcher(courseStateDiv),
            liveSwitcher = new components.Switcher(liveStateDiv),
            chatSwitcher = new components.Switcher(chatStateDiv);

        var liveRoomComponent = require('liveRoomComponent');

        if (userType === "teacher") {

            // 点击开始上课
            courseSwitcher.onDom.addEventListener("click", function () {
                console.log(courseSwitcher.isOn());
                if (!courseSwitcher.isOn() && courseId !== undefined) {
                    teacherHandler.startCourse(courseId, function () {
                        courseSwitcher.turnOn();
                    });
                }
            });
            // 点击下课
            courseSwitcher.offDom.addEventListener("click", function () {
                if (courseSwitcher.isOn() && courseId !== undefined) {
                    teacherHandler.endCourse(courseId, function () {
                        courseSwitcher.turnOff();
                    });
                }
            });

            // 开启Live
            liveSwitcher.onDom.addEventListener("click", function () {
                if (!liveSwitcher.isOn() && courseId !== undefined) {
                    liveRoomComponent.requestOpen();
                }
            });
            liveRoomComponent.on('open',function () {
                liveSwitcher.turnOn();
            });
            // 关闭Live
            liveSwitcher.offDom.addEventListener("click", function () {
                if (liveSwitcher.isOn() && courseId !== undefined) {
                    liveRoomComponent.requestClose();
                }
            });
            liveRoomComponent.on('closed',function () {
                liveSwitcher.turnOff();
            });

            // 开启Chat
            chatSwitcher.onDom.addEventListener("click", function () {
                if (!chatSwitcher.isOn() && courseId !== undefined) {
                    teacherHandler.startChat(courseId, function () {
                        chatSwitcher.turnOn();
                    });
                }
            });
            // 关闭Chat
            chatSwitcher.offDom.addEventListener("click", function () {
                if (chatSwitcher.isOn() && courseId !== undefined) {
                    teacherHandler.endChat(courseId, function () {
                        chatSwitcher.turnOff();
                    });
                }
            });


        } else if (userType === "student") {

        }


        // 初始化所有状态
        if (courseId) {
            userHandler.getCourse(courseId, function (data) {
                dragBoxLi.querySelector(".drag-box__scene").innerText = data.name;
                if (data.state)
                    courseSwitcher.turnOn()
                else
                    courseSwitcher.turnOff();
                if (data.live) {
                    liveSwitcher.turnOn();
                }
                else
                    liveSwitcher.turnOff();
                if (data.chat)
                    chatSwitcher.turnOn()
                else
                    chatSwitcher.turnOff();
                if (userType === "teacher" && data.live){
                    liveRoomComponent.requestOpen();
                }
            });

        }

        // 发言按钮
        if (userType === "student"){
            var sendMessageDom = document.querySelector("#send-message a");
            sendMessageDom.addEventListener("click",function () {
                require("popups").createSendMessagePopup();
            })
        }

        // 注销
        var logoutDom = document.getElementById('logout');
        logoutDom.addEventListener("click",function () {
            userHandler.logout(function () {
                alert("OK")
                $.cookie('courseId','',{ expires: 0 });
                $.cookie('type','',{ expires: 0 });
                location.reload()
            },function () {
                alert("注销失败")
            })
        })
    }

    return {
        initHTML: initHTML,
        initComponents: initComponents
    }
})